package com.ffcs.web.services;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ffcs.web.daos.implementations.BaogaoDao;
import com.ffcs.web.daos.implementations.PingguDao;
import com.ffcs.web.daos.implementations.QuestionDao;
import com.ffcs.web.daos.implementations.UserBaogaoDao;
import com.ffcs.web.model.dto.MyBaoGao;
import com.ffcs.web.model.dto.MyReportResponse;
import com.ffcs.web.model.entity.Baogao;
import com.ffcs.web.model.entity.Order;
import com.ffcs.web.model.entity.Pinggu;
import com.ffcs.web.model.entity.Question;
import com.ffcs.web.model.entity.UserBaogao;
import com.ffcs.web.model.entity.WxUser;

@Service
public class PingguService {

	@Autowired
	private PingguDao pingguDao;
	@Autowired
	private BaogaoDao baogaoDao;
	@Autowired
	private UserBaogaoDao userBaogaoDao;
	@Autowired
	private QuestionDao questionDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * 获取在线评估内容
	 * @param flag
	 * @return
	 */
	public Pinggu getPingguDesc(String flag) {
		Pinggu pinggu=pingguDao.get(Integer.parseInt(flag));
		return pinggu;
	}

	@Transactional
	public void updatePingguDesc(int pingguId) {
		Pinggu pinggu=pingguDao.get(pingguId);
		pinggu.setPingguCount(pinggu.getPingguCount()+1);
		pingguDao.save(pinggu);
	}

	/**
	 * 获取问题信息
	 * @param questionId
	 * @return
	 */
	public Question getQuestion(String questionId) {
		Question question=questionDao.get(Integer.parseInt(questionId));
		return question;
	}

	@Transactional
	public Baogao createBaogao(int score, int pingguId, WxUser wxUser) {

		List<Baogao> baogaoList=baogaoDao.findByPropertyName("pingguId",pingguId);
		for (Baogao baogao : baogaoList) {
			if(baogao.getLeftScore()<=score && baogao.getRightScore()>=score){
				UserBaogao userBaogao=new UserBaogao();
				userBaogao.setScore(score);
				userBaogao.setAddTime(new Date());
				userBaogao.setBaogaoId(baogao.getId());
				userBaogao.setPingguId(pingguId);
				userBaogao.setUserId(wxUser.getId());
				userBaogaoDao.save(userBaogao);
				return baogao;
			}
		}
		return null;
	}

	public Baogao getBaoGao(String baogaoId) {
		Baogao baogao=baogaoDao.get(Integer.parseInt(baogaoId));
		return baogao;
	}

	public MyReportResponse getMyReport(WxUser wxUser) {
		MyReportResponse myReportResponse=new MyReportResponse();
		myReportResponse.setResultCode("0");
		myReportResponse.setResultDesc("success");
		String sql="select u.baogaoId,u.addTime as baogaoTime,b.baogaoTitle,b.baogaoContent,u.pingguId as pingguId from wechat_user_baogao u,wechat_baogao b where u.userId='"+wxUser.getId()+"' and u.baogaoId=b.id order by u.addTime desc";
		List<MyBaoGao> myBaoGaoList=jdbcTemplate.query(sql, new BeanPropertyRowMapper<MyBaoGao>(MyBaoGao.class));
		myReportResponse.setMyBaoGaoList(myBaoGaoList);
		return myReportResponse;
	}
}
